package com.boxin.ims.modules.ad.web;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boxin.ims.modules.ad.entity.Advertisement;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

import java.io.IOException; 
import java.io.InputStream;  
import java.io.OutputStream;  
import java.net.URL; 
import java.net.URLConnection; 
import org.apache.commons.io.IOUtils;

@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/qq")
public class Test extends BaseController {
	
	/**
	   * 从服务器中下载图片
	   *
	   * @param fileName 图片地址
	   * @param response
	   * @return
	   */
	  @RequestMapping(value = "/download")
	  public void downloadMedia(HttpServletResponse response, HttpServletRequest request) {
	    InputStream inputStream = null;
	    OutputStream outputStream = null;
	    try {
	      //处理中文乱码
	      request.setCharacterEncoding("UTF-8");
	      String fileName = "http://hsm.hbweiyinqing.cn/huisimeng/userfiles/huisimeng/admin_huisi_1481880567699.jpg";
	      fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
	      //处理浏览器兼容
	      response.setContentType("application/msexcel;charset=utf-8");//定义输出类型
	      Enumeration enumeration = request.getHeaders("User-Agent");
	      String browserName = (String) enumeration.nextElement();
	      boolean isMSIE = browserName.contains("MSIE");
	      if (isMSIE) {
	        response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF8"));
	      } else {
	        response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
	      }
	      //url地址如果存在空格，会导致报错！  解决方法为：用+或者%20代替url参数中的空格。
	      fileName = fileName.replace(" ", "%20");
	      //图片下载
	      URL url = new URL(fileName);
	      URLConnection conn = url.openConnection();
	      outputStream = response.getOutputStream();
	      inputStream = conn.getInputStream();
	      IOUtils.copy(inputStream, outputStream);
	    } catch (IOException e) {
	      System.err.println(e);
	    }finally { 
	      IOUtils.closeQuietly(inputStream); 
	      IOUtils.closeQuietly(outputStream); 
	   } 
	  }
}
